#include "Player.h"
#include "Space.h"

const FunctionReturn Player::Move(int Dir)
{
	if(m_bMoving != true)
	{
		SpacesToMove = Dir;
		Space* tempCur = CurrentSpace;
		//if the 0 is passed it the function fails
		if(Dir != 0)
		{
					//if its less than zero the function sets the
		// for loop to run the backwards option, but still
		// keep dir as a positive number
			if(Dir < 0)
			{
				Dir *= -1;
				for(int i = 0;i < Dir;++i)
				{
					if(tempCur->PrevSpace() != NULL)
						tempCur = tempCur->MoveBackward();
					else
					{
						SpacesToMove = 0;
						MovingTo = CurrentSpace;
						return MISSING_DATA;
					}
				}
			}
			else
			{
				for(int i = 0;i < Dir;++i)
				{
					if(tempCur->NextSpace() != NULL)
						tempCur = tempCur->MoveForward();
						//reset position if it would go to far
				// return fail reason
					else
					{
						SpacesToMove = 0;
						MovingTo = CurrentSpace;
						return MISSING_DATA;
					}
				}
			}

			MovingTo = tempCur;
			m_bMoving = true;
			AddMaterial(eInvMaterials(rand() % MAT_NULL),1);
			return SUCCEED;
		}
		return INVALID_DATA;
	}
	return SUCCEED;
}

//Steps through each space after the one currently on until it finds one TILE's tile type
// if it finds a space with the next space being null and hasn't found a matching tile
// it returns the player to the original position and returns out of the function
const FunctionReturn Player::Move(eTileType TILE)
{
	if(m_bMoving != true)
	{	
		Space* tempCur = CurrentSpace;
				//Used a do while because it simplified things
	// runs through once first to avoid checking the space
	// that the player is currently on. 
		do
		{
			if(tempCur->NextSpace() != NULL)
				tempCur = tempCur->MoveForward();
			else
			{
				MovingTo = CurrentSpace;
				return MISSING_DATA;
			}
		}while(tempCur->GetMaterialType() != TILE);

		MovingTo = tempCur;
		m_bMoving = true;
		AddMaterial(eInvMaterials(rand() % MAT_NULL),1);
	}
	return SUCCEED;
}

const bool Player::IfAtEnd()
{
	if(CurrentSpace->ifLastSpace() == true)
		return true;
	return false;
}

void Player::Update(float DeltaT)
{
	if(m_bMoving)
		Step(DeltaT);
}

const FunctionReturn Player::Step(float DeltaT)
{
	m_vPosition.x = m_vPosition.x + (MovingTo->m_vPosition.x - m_vPosition.x) * DeltaT * 5;
	m_vPosition.y = m_vPosition.y + (MovingTo->m_vPosition.y - m_vPosition.y) * DeltaT * 5;
	if(abs(MovingTo->m_vPosition.x - m_vPosition.x) < .5
	&& abs(MovingTo->m_vPosition.y - m_vPosition.y) < .5)
	{
		MovingTo = MovingTo->LandOn();
		if(abs(MovingTo->m_vPosition.x - m_vPosition.x) < .5
		&& abs(MovingTo->m_vPosition.y - m_vPosition.y) < .5)
		{
			CurrentSpace = MovingTo;
			m_bMoving = false;
		}
	}
	return SUCCEED;
}

//Used by Item.h in Use()
FunctionReturn UseTNT(BaseObject* User)
{
	((Player*)User)->Move(10);
	return SUCCEED;
}

FunctionReturn UsePickaxe(BaseObject* User)
{
	((Player*)User)->Move(-10);
	return SUCCEED;
}

//Used by Player.h in Construct()
FunctionReturn CanMakeTNT(ResourceBag* RB)
{
	if(RB->CheckSulfur() >= 3)
	{
		RB->RemoveSulfur(3);
		return SUCCEED;
	}
	return FAIL;
}

FunctionReturn CanMakePickaxe(ResourceBag* RB)
{
	if(RB->CheckIron() >= 3)
	{
		RB->RemoveIron(3);
		return SUCCEED;
	}
	return FAIL;
}